fn main() {
    println!("Hello, world!");
    let mut s = String::from("Hello world!");
    let word = first_world(&s);

    //s.clear();

    println!("first word: {word}");

    let s = String::from("You are a good boy!");

    // first_world2 也适用于 String 的引用，
    // 这等价于整个 String 的 slice
    let word = first_world2(&s);
    let word = first_world2(&s[0..10]);


    // 其他更通用的slice
    let a = [1, 2, 3, 4, 5];
    let slice = &a[2..4];
    assert_eq!(slice, &[2, 3]);
}

// 字符串slice
fn first_world(s: &String) -> &str {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i]
        }
    }

    &s
}

fn first_world2(s: &str) -> &str {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i]
        }
    }

    &s
}